package core;

import net.ByteBuffer;
import net.ByteReader;

/**
 * Any entity which can be viewed on the screen.
 * 
 * @author Jason
 * 
 */
public abstract class ViewableEntity extends Entity {

	private static final int SIZE_THRESHOLD = 2;
	private static final float HEADING_THRESHOLD = .025f;

	/**
	 * The layer this entity exists on.
	 */
	public byte layer;

	/**
	 * The amount of space this entity takes up.
	 */
	public short width, height;

	/**
	 * The direction this entity is facing in radians. <br>
	 * <br>
	 * This number should be between 0 and 2 PI.
	 */
	public float heading;

	public ViewableEntity() {
		width = getWidth();
		height = getHeight();
	}

	/**
	 * Gets the width of this entity.
	 * 
	 * @return The number of pixels in width (used for bounds checking)
	 */
	public abstract short getWidth();

	/**
	 * Gets the height of this entity.
	 * 
	 * @return The number of pixels in height (used for bounds checking)
	 */
	public abstract short getHeight();

	@Override
	public void readAttributes(ByteReader reader) {
		super.readAttributes(reader);
		layer = reader.read();
		width = reader.readShort();
		height = reader.readShort();
		heading = reader.readFloat();
	}

	@Override
	protected void readAttributes(Entity e) {
		super.readAttributes(e);
		ViewableEntity v = (ViewableEntity) e;
		layer = v.layer;
		width = v.width;
		height = v.height;
		heading = v.heading;
	}

	@Override
	protected void writeAttributes(ByteBuffer buffer) {
		super.writeAttributes(buffer);
		buffer.add(layer);
		buffer.addShort(width);
		buffer.addShort(height);
		buffer.addFloat(heading);
	}

	@Override
	public boolean equals(Entity e) {
		if (super.equals(e) == false)
			return false;
		ViewableEntity v = (ViewableEntity) e;
		return layer == v.layer && Math.abs(width - v.width) < SIZE_THRESHOLD
				&& Math.abs(height - v.height) < SIZE_THRESHOLD
				&& Math.abs(heading - v.heading) < HEADING_THRESHOLD;
	}

}
